/************************************************************************************
* HistoryModel.cpp                                                                  *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "HistoryModel.h"

#include <QDateTime>

using namespace Yamp;

HistoryModel::HistoryModel(YContext *context,
                           HistoryModel::HistoryType historyType,
                           QObject *parent):
    m_context(context),
    m_historyType(historyType) {

}


int HistoryModel::columnCount(const QModelIndex &) const {
    return 5;
}


QVariant HistoryModel::data(const QModelIndex &index, int role) const {
    if(m_items == NULL || ! index.isValid() || index.row() >= m_items->size()) {
        return QVariant();
    }
    if (role == Qt::DisplayRole) {
        QString id = m_items->at(index.row())->trackId();
        switch(index.column()) {
        case 4:
            if(m_historyType == HistoryModel::RecentlyPlayed) {
                quint64 milliSec = m_context->ydb()->retriever()->retrievePlayedTime(id);
                return QDateTime::fromMSecsSinceEpoch(milliSec);
            }
            if(m_historyType == HistoryModel::RecentlyAdded) {
                quint64 milliSec = m_context->ydb()->retriever()->retrieveAddedTime(id);
                return QDateTime::fromMSecsSinceEpoch(milliSec);
            }
        default: return PartialCollectionModel::data(index, role);
        }
    }
    else {
       return PartialCollectionModel::data(index, role);
    }
    return QVariant();
}






QVariant HistoryModel::headerData(int section, Qt::Orientation ort, int role) const {
    if (role == Qt::DisplayRole) {
        switch(section) {
        case 4: return m_historyType == HistoryModel::RecentlyPlayed?
                        "Last Played":"Added Time";
        default: return PartialCollectionModel::headerData(section, ort, role);
        }
    }
    return QVariant();
}
